package utils;

import java.sql.*;

public class DBUtils {
	public static Connection getConnection() throws SQLException {
		try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			return DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/electricbicycle?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8",
					"root",
					"root"
			);
		} catch (ClassNotFoundException e) {
			throw new SQLException("数据库驱动加载失败", e);
		} catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

	public static void close(Connection conn, PreparedStatement pstmt, ResultSet rs) {
		try {
			if (rs != null) rs.close();
			if (pstmt != null) pstmt.close();
			if (conn != null) conn.close();
		} catch (SQLException e) {
			System.err.println("资源关闭失败: " + e.getMessage());
		}
	}
}